package mindshare;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import fragments.FriendsFragment;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.ArrayAdapter;

public class LoadContent extends AsyncTask<String, String, String> {
	protected ProgressDialog pDialog;
	protected static String url = "http://mindshare.comli.com/";
	protected String script;
	protected String data;
	public List<String> contentList= new ArrayList<String>();
	protected List<NameValuePair> param;
	protected JSONParser jParser = new JSONParser();
	protected ArrayAdapter <String> adpContent;
	protected Context c;
	
	public LoadContent(Context context, List<NameValuePair> param, String data){
		c = context;
		this.param=param;
		this.data=data;
	}
	
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(c);
		pDialog.setMessage("Loading "+ data + ". Please wait...");
        pDialog.show();
    }

	protected String doInBackground(String... args) {
		if (data=="cities")	script= "obtainCities.php";
		else if (data=="countries")	script= "obtainCountries.php";
		else if (data=="password")	script= "getPassword.php";
//		Log.d("Url",url+script);
		JSONObject json = jParser.makeHttpRequest(url+script, param);
		JSONArray countries;
		contentList.clear();
		try {
			countries = json.getJSONArray(data);
			for (int i = 0; i < countries.length(); i++) {
				String c = countries.getString(i);
				contentList.add(c);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	
    protected void onPostExecute(String file_url) {
    	adpContent= new ArrayAdapter <String>( (Activity)c, android.R.layout.simple_list_item_1, android.R.id.text1, contentList);
    	adpContent.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	
        pDialog.dismiss();
    }
}